import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 引入多个动态路由模块
import departmentsRouter from './modules/departments'
import settingRouter from './modules/settings'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permissions'

import approvalsRouter from './modules/approvals'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import socialRouter from './modules/social_securitys'

// 动态路由表
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,

  approvalsRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter
]

// 静态路由
export const constantRoutes = [
  {
    path: '/import',
    component: () => import(/* webpackChunkName:"import"*/'@/views/import/index'),
    hidden: true
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName:"login"*/'@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import(/* webpackChunkName:"404"*/'@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import(/* webpackChunkName:"dashboard"*/'@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  }
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }), // 管理滚动行为 如果出现滚动 切换就让 让页面回到顶部
  // 指定路由规则，合并静态路由和动态路由生成一种的路由表
  // routes: [...constantRoutes, ...asyncRoutes]
  routes: [...constantRoutes] // 临时合并所有的路由
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重新设置路由的可匹配路径
}

export default router
